package com.feiyu.service.impl;

import com.feiyu.aop.FeiYuLog;
import com.feiyu.aop.MyLog;
import com.feiyu.mapper.DeptMapper;
import com.feiyu.mapper.EmpMapper;
import com.feiyu.pojo.Dept;
import com.feiyu.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    @FeiYuLog    //将查看的日志写入 数据库
     @MyLog    //定义查看时候注解
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }


    @FeiYuLog
    @Transactional(rollbackFor = Exception.class)   //设置所有异常   //开启事务          默认是只有运行时异常进行回滚
    @Override
    public void delete(Integer id) {


        deptMapper.delete(id);
//根据部门id删除员工

     //   int i = 1 / 0;
        empMapper.deleteByDeptId(id);
    }

  @FeiYuLog
    @Override
    public void add(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.add(dept);
    }

    @FeiYuLog
    @Override
    public Dept list02(Integer id) {

        return deptMapper.list02(id);

    }

    @Override
    public void update(Dept dept) {

        dept.setUpdateTime(LocalDateTime.now());

        deptMapper.update(dept);
    }
}
